/*
 * Copyright (C) 2010 M. Hautle.
 * 
 * This file is part of SimpleEL.
 * 
 * SimpleEL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * SimpleEL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with SimpleEL. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.simpleel.speed;

import java.lang.reflect.*;
import ch.simpleel.data.*;
import ch.simpleel.internal.reflection.*;

/**
 * Speed test with native calls
 * 
 * @author M. Hautle
 */
public class Reflection extends Common {
    private final Method[] DOMACC = create(TheDom.class, PROPERTIES);

    private final Method[] OTHERACC = create(AOtherDom.class, PROPERTIES);

    private static Method[] create(Class c, String[] names) {
        final Method[] res = new Method[names.length];
        for (int i = 0; i < names.length; i++)
            res[i] = ReflectionHelper.getGetter(c, names[i]);
        return res;
    }

    /**
     * VM Args:<br>
     * 
     * <pre>
     * -server / -client
     * -Xms512m -Xmx512m
     * -XX:+PrintCompilation 
     * -verbose:gc
     * </pre>
     * 
     * @param args
     */
    public static void main(String[] args) {
        new Reflection().executeBenchmark();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean executeChecks(TheDom dom, AOtherDom dom2) {
        return checkProperties(dom, DOMACC) && checkProperties(dom2, OTHERACC);
    }

    private boolean checkProperties(Object o, Method[] acc) {
        try {
            boolean res = true;
            for (int i = 0; i < acc.length; i++)
                res &= VALUES[i].equals(acc[i].invoke(o, (Object[]) null));
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
